﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eneta.Portal.Controls;
using Microsoft.SharePoint.Publishing;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Eneta.Portal.Literature.Controls;
using Eneta.Portal.Common.Helpers;
using Eneta.Portal.Common.Dto;
using System.Web.UI.WebControls;
using Eneta.Portal.Common;
using Eneta.Portal.Common.Definitions;
using Microsoft.SharePoint.Utilities;
using Eneta.Portal.Common.Repository;
using Constants;

namespace Eneta.Portal.Literature.Pages
{
    public class MagazineDetailPage : PublishingLayoutPage
    {
        protected HtmlAnchor posterProfileLink;
        protected HtmlImage posterImage;
        protected HtmlGenericControl posterName;
        protected HtmlGenericControl postDate;
        protected HtmlGenericControl shortDescription;
        protected HtmlGenericControl description;
        protected HtmlImage magazinePicture;
        protected HtmlGenericControl publishingFrequency;
        protected HtmlAnchor additionalInfoURL;

        protected HtmlGenericControl AcceptPostingContainer;
        protected Label StatusLabel;
        protected Button btnAccept;
        protected Button btnReject;
        protected Button btnChange;
        protected TagsViewControl magazineTags;
        protected TagsSelectionControl magazineTagsSelection;
        protected TextBox rejectReasonField;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.btnAccept.Click += new EventHandler(btnAccept_Click);
            this.btnReject.Click += new EventHandler(btnReject_Click);
            btnChange.Click += new EventHandler(btnChange_Click);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (IsPostBack)
                return;

            BindItem();

            AcceptPostingContainer.Visible = false;
            
            if (IsNotAnonymous() && IsSiteAdmin())
            {
                AcceptPostingContainer.Visible = true;
                var moderationInfo = SPContext.Current.ListItem.ModerationInformation;

                if (moderationInfo.Status == SPModerationStatusType.Approved)
                {
                    StatusLabel.Text = "Kinnitatud";
                    this.btnAccept.Visible = false;
                }
                else if (moderationInfo.Status == SPModerationStatusType.Denied)
                {
                    StatusLabel.Text = "Tagasilükatud";
                    this.btnReject.Visible = false;
                }
                else
                    StatusLabel.Text = "Ootel";
            }
        }

        private bool IsNotAnonymous()
        {
            return SPContext.Current.Web.CurrentUser != null;
        }

        private bool IsSiteAdmin()
        {
            return SPContext.Current.Web.CurrentUser.IsSiteAdmin;
        }

        void btnReject_Click(object sender, EventArgs e)
        {
            AcceptItem(false);
            Response.Redirect(Request.RawUrl, false);
        }

        void btnAccept_Click(object sender, EventArgs e)
        {
            AcceptItem(true);
            Response.Redirect(Request.RawUrl, false);
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            string url = "/oppimine/ajakirjad/edit.aspx?ID=" + SPContext.Current.ListItem.ID;
            Response.Redirect(url);
        }

        private void AcceptItem(bool accept)
        {
            var repository = new LiteratureRepository(SPContext.Current.Web);
            if (accept)
            {
                repository.ApproveMagazine(SPContext.Current.ListItem.ID, SPContext.Current.Web.ServerRelativeUrl, this.rejectReasonField.Text, this.magazineTagsSelection.GetSelectedKeywords());
            }
            else
            {
                repository.DenieMagazine(SPContext.Current.ListItem.ID, SPContext.Current.Web.ServerRelativeUrl, this.rejectReasonField.Text);
            }
        }

        //Some are binded with databound controls
        private void BindItem()
        {
            LiteratureDto bookDto = new LiteratureDto(SPContext.Current.Web, SPContext.Current.ListItem);

            posterProfileLink.HRef = UrlHelper.GetProfileUrl(bookDto.PosterProfileId);
            posterName.InnerText = bookDto.Author;
            posterImage.Src = UrlHelper.GetUserImageHandlerUrl(16, bookDto.PosterProfileId);
            posterImage.Alt = bookDto.Title;
            postDate.InnerText = bookDto.PostedDate.ToString("dd.MM.yyyy");

            description.InnerHtml = bookDto.Summary;
            magazinePicture.Alt = bookDto.Title;
            magazinePicture.Src = bookDto.LiteratureImageUrl;
            magazineTags.Keywords = bookDto.Keywords;

            var publishingFrequencyValue = SPContext.Current.ListItem[Fields.PublishingFrequency.Name] as string;
            if (!string.IsNullOrEmpty(publishingFrequencyValue))
            {
                publishingFrequency.InnerText = publishingFrequencyValue.Replace("|", " korda ");
            }

            Logger.LogInfo(bookDto.AdditionalInfoURL + " lisainfo url väli", "MagazineDetailPage");
            if (!string.IsNullOrEmpty(bookDto.AdditionalInfoURL))
            {
                additionalInfoURL.HRef = bookDto.AdditionalInfoURL;
                additionalInfoURL.Visible = true;
            }
        }
    }
}
